home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / cpu-ns32k.c < prev    next >
C/C++ Source or Header  |  1994-08-08  |  27KB  |  880 lines

  1. /* BFD support for the ns32k architecture.
  2.    Copyright (C) 1990, 1991, 1994 Free Software Foundation, Inc.
  3.    Almost totally rewritten by Ian Dall from initial work
  4.    by Andrew Cagney.
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include "libbfd.h"
  25.  
  26. void bfd_ns32k_arch PARAMS ((void));
  27. long ns32k_get_displacement PARAMS ((bfd_byte *buffer, long offset, long size));
  28. int ns32k_put_displacement PARAMS ((long value, bfd_byte *buffer, long offset, long size));
  29. long ns32k_get_immediate PARAMS ((bfd_byte *buffer, long offset, long size));
  30. int ns32k_put_immediate  PARAMS ((long value, bfd_byte *buffer, long offset, long size));
  31. bfd_reloc_status_type
  32.   ns32k_reloc_disp PARAMS ((bfd *abfd, arelent *reloc_entry,
  33.            struct symbol_cache_entry *symbol,
  34.            PTR data,
  35.            asection *input_section,
  36.            bfd *output_bfd,
  37.            char **error_message));
  38. bfd_reloc_status_type
  39.   ns32k_reloc_imm  PARAMS ((bfd *abfd,
  40.            arelent *reloc_entry,
  41.            struct symbol_cache_entry *symbol,
  42.            PTR data,
  43.            asection *input_section,
  44.            bfd *output_bfd,
  45.            char **error_message));
  46. bfd_reloc_status_type ns32k_final_link_relocate  PARAMS (( const reloc_howto_type *howto,
  47.                  bfd *input_bfd,
  48.                  asection *input_section,
  49.                  bfd_byte *contents,
  50.                  bfd_vma address,
  51.                  bfd_vma value,
  52.                  bfd_vma addend ));
  53. bfd_reloc_status_type ns32k_relocate_contents  PARAMS ((const reloc_howto_type *howto,
  54.                             bfd *input_bfd,
  55.                             bfd_vma relocation,
  56.                             bfd_byte *location));
  57.  
  58. int bfd_default_scan_num_mach();
  59.  
  60. #define N(machine, printable, d)  \
  61. {  32, 32, 8, bfd_arch_ns32k, machine, "ns32k",printable,3,d,bfd_default_compatible,bfd_default_scan, 0, }
  62.  
  63. static bfd_arch_info_type arch_info_struct[] =
  64.   N(32032,"ns32k:32032",false),
  65.   N(32532,"ns32k:32532",true), /* the word ns32k will match this too */
  66.   { 0 }
  67. }
  68. ;
  69.  
  70.  
  71. void bfd_ns32k_arch()
  72. {
  73.   unsigned int i;
  74.   for (i = 0; arch_info_struct[i].bits_per_word; i++)
  75.       {
  76.     bfd_arch_linkin(&arch_info_struct[i]);
  77.       }
  78. }
  79.  
  80. static long
  81. ns32k_sign_extend(value, bits)
  82.      int value;
  83.      int bits;
  84. {
  85.   value = value & ((1 << bits) - 1);
  86.   return (value & (1 << (bits-1))
  87.       ? value | (~((1 << bits) - 1))
  88.       : value);
  89. }
  90.  
  91. long
  92. ns32k_get_displacement(buffer, offset, size)
  93.      bfd_byte *buffer;
  94.      long offset;
  95.      long size;
  96. {
  97.   long value;
  98.   buffer += offset;
  99.   switch (size)
  100.     {
  101.     case 1:
  102.       value = ns32k_sign_extend (*buffer, 7);
  103.       break;
  104.     case 2:
  105.       value = ns32k_sign_extend(*buffer++, 6);
  106.       value = (value << 8) | (0xff & *buffer);
  107.       break;
  108.     case 4:
  109.       value = ns32k_sign_extend(*buffer++, 6);
  110.       value = (value << 8) | (0xff & *buffer++);
  111.       value = (value << 8) | (0xff & *buffer++);
  112.       value = (value << 8) | (0xff & *buffer);
  113.       break;
  114.     }
  115.   return value;
  116. }
  117.  
  118. int
  119. ns32k_put_displacement(value, buffer, offset, size)
  120.      long value;
  121.      bfd_byte *buffer;
  122.      long offset;
  123.      long size;
  124. {
  125.   buffer += offset;
  126.   switch (size)
  127.     {
  128.     case 1:
  129.       if (value < -64 || value > 63)
  130.     return -1;
  131.       value&=0x7f;
  132.       *buffer++=value;
  133.       break;
  134.     case 2:
  135.       if (value < -8192 || value > 8191)
  136.     return -1;
  137.       value&=0x3fff;
  138.       value|=0x8000;
  139.       *buffer++=(value>>8);
  140.       *buffer++=value;
  141.       break;
  142.     case 4:
  143.       if (value < -0x1f000000 || value >= 0x20000000)
  144.     return -1;
  145.       value|=0xc0000000;
  146.       *buffer++=(value>>24);
  147.       *buffer++=(value>>16);
  148.       *buffer++=(value>>8);
  149.       *buffer++=value;
  150.       break;
  151.     default:
  152.       return -1;
  153.   }
  154.   return 0;
  155. }
  156.  
  157. long
  158. ns32k_get_immediate(buffer, offset, size)
  159.      bfd_byte *buffer;
  160.      long offset;
  161.      long size;
  162. {
  163.   long value = 0;
  164.   buffer += offset;
  165.   switch (size)
  166.     {
  167.     case 4:
  168.       value = (value << 8) | (*buffer++ & 0xff);
  169.     case 3:
  170.       value = (value << 8) | (*buffer++ & 0xff);
  171.     case 2:
  172.       value = (value << 8) | (*buffer++ & 0xff);
  173.     case 1:
  174.       value = (value << 8) | (*buffer++ & 0xff);
  175.     }
  176.   return value;
  177. }
  178.  
  179. int
  180. ns32k_put_immediate (value, buffer, offset, size)
  181.      long value;
  182.      bfd_byte *buffer;
  183.      long offset;
  184.      long size;
  185. {
  186.   buffer += offset + size - 1;
  187.   switch (size)
  188.     {
  189.     case 4:
  190.       *buffer-- = (value & 0xff); value >>= 8;
  191.     case 3:
  192.       *buffer-- = (value & 0xff); value >>= 8;
  193.     case 2:
  194.       *buffer-- = (value & 0xff); value >>= 8;
  195.     case 1:
  196.       *buffer-- = (value & 0xff); value >>= 8;
  197.     }
  198.   return 0;
  199. }
  200.  
  201. /* This is just like the standard perform_relocation except we
  202.  * use get_data and put_data which know about the ns32k
  203.  * storage methods.
  204.  * This is probably a lot more complicated than it needs to be!
  205.  */
  206. static bfd_reloc_status_type
  207. do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
  208.         error_message, get_data, put_data)
  209.      bfd *abfd;
  210.      arelent *reloc_entry;
  211.      struct symbol_cache_entry *symbol;
  212.      PTR data;
  213.      asection *input_section;
  214.      bfd *output_bfd;
  215.      char **error_message;
  216.      long (*get_data)();
  217.      int (*put_data)();
  218. {
  219.   int overflow = 0;
  220.   bfd_vma relocation;
  221.   bfd_reloc_status_type flag = bfd_reloc_ok;
  222.   bfd_size_type addr = reloc_entry->address;
  223.   bfd_vma output_base = 0;
  224.   const reloc_howto_type *howto = reloc_entry->howto;
  225.   asection *reloc_target_output_section;
  226.  
  227.   if ((symbol->section == &bfd_abs_section)
  228.       && output_bfd != (bfd *) NULL)
  229.     {
  230.       reloc_entry->address += input_section->output_offset;
  231.       return bfd_reloc_ok;
  232.     }
  233.  
  234.   /* If we are not producing relocateable output, return an error if
  235.      the symbol is not defined.  An undefined weak symbol is
  236.      considered to have a value of zero (SVR4 ABI, p. 4-27).  */
  237.   if (symbol->section == &bfd_und_section
  238.       && (symbol->flags & BSF_WEAK) == 0
  239.       && output_bfd == (bfd *) NULL)
  240.     flag = bfd_reloc_undefined;
  241.  
  242.  
  243.   /* Is the address of the relocation really within the section?  */
  244.   if (reloc_entry->address > input_section->_cooked_size)
  245.     return bfd_reloc_outofrange;
  246.  
  247.   /* Work out which section the relocation is targetted at and the
  248.      initial relocation command value.  */
  249.  
  250.   /* Get symbol value.  (Common symbols are special.)  */
  251.   if (bfd_is_com_section (symbol->section))
  252.     relocation = 0;
  253.   else
  254.     relocation = symbol->value;
  255.  
  256.  
  257.   reloc_target_output_section = symbol->section->output_section;
  258.  
  259.   /* Convert input-section-relative symbol value to absolute.  */
  260.   if (output_bfd && howto->partial_inplace == false)
  261.     output_base = 0;
  262.   else
  263.     output_base = reloc_target_output_section->vma;
  264.  
  265.   relocation += output_base + symbol->section->output_offset;
  266.  
  267.   /* Add in supplied addend.  */
  268.   relocation += reloc_entry->addend;
  269.  
  270.   /* Here the variable relocation holds the final address of the
  271.      symbol we are relocating against, plus any addend.  */
  272.  
  273.   if (howto->pc_relative == true)
  274.     {
  275.       /* This is a PC relative relocation.  We want to set RELOCATION
  276.      to the distance between the address of the symbol and the
  277.      location.  RELOCATION is already the address of the symbol.
  278.  
  279.      We start by subtracting the address of the section containing
  280.      the location.
  281.  
  282.      If pcrel_offset is set, we must further subtract the position
  283.      of the location within the section.  Some targets arrange for
  284.      the addend to be the negative of the position of the location
  285.      within the section; for example, i386-aout does this.  For
  286.      i386-aout, pcrel_offset is false.  Some other targets do not
  287.      include the position of the location; for example, m88kbcs,
  288.      or ELF.  For those targets, pcrel_offset is true.
  289.  
  290.      If we are producing relocateable output, then we must ensure
  291.      that this reloc will be correctly computed when the final
  292.      relocation is done.  If pcrel_offset is false we want to wind
  293.      up with the negative of the location within the section,
  294.      which means we must adjust the existing addend by the change
  295.      in the location within the section.  If pcrel_offset is true
  296.      we do not want to adjust the existing addend at all.
  297.  
  298.      FIXME: This seems logical to me, but for the case of
  299.      producing relocateable output it is not what the code
  300.      actually does.  I don't want to change it, because it seems
  301.      far too likely that something will break.  */
  302.  
  303.       relocation -=
  304.     input_section->output_section->vma + input_section->output_offset;
  305.  
  306.       if (howto->pcrel_offset == true)
  307.     relocation -= reloc_entry->address;
  308.     }
  309.  
  310.   if (output_bfd != (bfd *) NULL)
  311.     {
  312.       if (howto->partial_inplace == false)
  313.     {
  314.       /* This is a partial relocation, and we want to apply the relocation
  315.          to the reloc entry rather than the raw data. Modify the reloc
  316.          inplace to reflect what we now know.  */
  317.       reloc_entry->addend = relocation;
  318.       reloc_entry->address += input_section->output_offset;
  319.       return flag;
  320.     }
  321.       else
  322.     {
  323.       /* This is a partial relocation, but inplace, so modify the
  324.          reloc record a bit.
  325.  
  326.          If we've relocated with a symbol with a section, change
  327.          into a ref to the section belonging to the symbol.  */
  328.  
  329.       reloc_entry->address += input_section->output_offset;
  330.  
  331.       /* WTF?? */
  332.       if (abfd->xvec->flavour == bfd_target_coff_flavour
  333.           && strcmp (abfd->xvec->name, "aixcoff-rs6000") != 0)
  334.         {
  335. #if 1
  336.           /* For m68k-coff, the addend was being subtracted twice during
  337.          relocation with -r.  Removing the line below this comment
  338.          fixes that problem; see PR 2953.
  339.  
  340. However, Ian wrote the following, regarding removing the line below,
  341. which explains why it is still enabled:  --djm
  342.  
  343. If you put a patch like that into BFD you need to check all the COFF
  344. linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
  345. SCO); see coff_i386_reloc in coff-i386.c where I worked around the
  346. problem in a different way.  There may very well be a reason that the
  347. code works as it does.
  348.  
  349. Hmmm.  The first obvious point is that bfd_perform_relocation should
  350. not have any tests that depend upon the flavour.  It's seem like
  351. entirely the wrong place for such a thing.  The second obvious point
  352. is that the current code ignores the reloc addend when producing
  353. relocateable output for COFF.  That's peculiar.  In fact, I really
  354. have no idea what the point of the line you want to remove is.
  355.  
  356. A typical COFF reloc subtracts the old value of the symbol and adds in
  357. the new value to the location in the object file (if it's a pc
  358. relative reloc it adds the difference between the symbol value and the
  359. location).  When relocating we need to preserve that property.
  360.  
  361. BFD handles this by setting the addend to the negative of the old
  362. value of the symbol.  Unfortunately it handles common symbols in a
  363. non-standard way (it doesn't subtract the old value) but that's a
  364. different story (we can't change it without losing backward
  365. compatibility with old object files) (coff-i386 does subtract the old
  366. value, to be compatible with existing coff-i386 targets, like SCO).
  367.  
  368. So everything works fine when not producing relocateable output.  When
  369. we are producing relocateable output, logically we should do exactly
  370. what we do when not producing relocateable output.  Therefore, your
  371. patch is correct.  In fact, it should probably always just set
  372. reloc_entry->addend to 0 for all cases, since it is, in fact, going to
  373. add the value into the object file.  This won't hurt the COFF code,
  374. which doesn't use the addend; I'm not sure what it will do to other
  375. formats (the thing to check for would be whether any formats both use
  376. the addend and set partial_inplace).
  377.  
  378. When I wanted to make coff-i386 produce relocateable output, I ran
  379. into the problem that you are running into: I wanted to remove that
  380. line.  Rather than risk it, I made the coff-i386 relocs use a special
  381. function; it's coff_i386_reloc in coff-i386.c.  The function
  382. specifically adds the addend field into the object file, knowing that
  383. bfd_perform_relocation is not going to.  If you remove that line, then
  384. coff-i386.c will wind up adding the addend field in twice.  It's
  385. trivial to fix; it just needs to be done.
  386.  
  387. The problem with removing the line is just that it may break some
  388. working code.  With BFD it's hard to be sure of anything.  The right
  389. way to deal with this is simply to build and test at least all the
  390. supported COFF targets.  It should be straightforward if time and disk
  391. space consuming.  For each target:
  392.     1) build the linker
  393.     2) generate some executable, and link it using -r (I would
  394.        probably use paranoia.o and link against newlib/libc.a, which
  395.        for all the supported targets would be available in
  396.        /usr/cygnus/progressive/H-host/target/lib/libc.a).
  397.     3) make the change to reloc.c
  398.     4) rebuild the linker
  399.     5) repeat step 2
  400.     6) if the resulting object files are the same, you have at least
  401.        made it no worse
  402.     7) if they are different you have to figure out which version is
  403.        right
  404. */
  405.           relocation -= reloc_entry->addend;
  406. #endif
  407.           reloc_entry->addend = 0;
  408.         }
  409.       else
  410.         {
  411.           reloc_entry->addend = relocation;
  412.         }
  413.     }
  414.     }
  415.   else
  416.     {
  417.       reloc_entry->addend = 0;
  418.     }
  419.  
  420.   /* FIXME: This overflow checking is incomplete, because the value
  421.      might have overflowed before we get here.  For a correct check we
  422.      need to compute the value in a size larger than bitsize, but we
  423.      can't reasonably do that for a reloc the same size as a host
  424.      machine word.
  425.      FIXME: We should also do overflow checking on the result after
  426.      adding in the value contained in the object file.  */
  427.   if (howto->complain_on_overflow != complain_overflow_dont)
  428.     {
  429.       bfd_vma check;
  430.  
  431.       /* Get the value that will be used for the relocation, but
  432.      starting at bit position zero.  */
  433.       if (howto->rightshift > howto->bitpos)
  434.     check = relocation >> (howto->rightshift - howto->bitpos);
  435.       else
  436.     check = relocation << (howto->bitpos - howto->rightshift);
  437.       switch (howto->complain_on_overflow)
  438.     {
  439.     case complain_overflow_signed:
  440.       {
  441.         /* Assumes two's complement.  */
  442.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  443.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  444.  
  445.         /* The above right shift is incorrect for a signed value.
  446.            Fix it up by forcing on the upper bits.  */
  447.         if (howto->rightshift > howto->bitpos
  448.         && (bfd_signed_vma) relocation < 0)
  449.           check |= ((bfd_vma) - 1
  450.             & ~((bfd_vma) - 1
  451.                 >> (howto->rightshift - howto->bitpos)));
  452.         if ((bfd_signed_vma) check > reloc_signed_max
  453.         || (bfd_signed_vma) check < reloc_signed_min)
  454.           flag = bfd_reloc_overflow;
  455.       }
  456.       break;
  457.     case complain_overflow_unsigned:
  458.       {
  459.         /* Assumes two's complement.  This expression avoids
  460.            overflow if howto->bitsize is the number of bits in
  461.            bfd_vma.  */
  462.         bfd_vma reloc_unsigned_max =
  463.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  464.  
  465.         if ((bfd_vma) check > reloc_unsigned_max)
  466.           flag = bfd_reloc_overflow;
  467.       }
  468.       break;
  469.     case complain_overflow_bitfield:
  470.       {
  471.         /* Assumes two's complement.  This expression avoids
  472.            overflow if howto->bitsize is the number of bits in
  473.            bfd_vma.  */
  474.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  475.  
  476.         if (((bfd_vma) check & ~reloc_bits) != 0
  477.         && ((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  478.           {
  479.         /* The above right shift is incorrect for a signed
  480.            value.  See if turning on the upper bits fixes the
  481.            overflow.  */
  482.         if (howto->rightshift > howto->bitpos
  483.             && (bfd_signed_vma) relocation < 0)
  484.           {
  485.             check |= ((bfd_vma) - 1
  486.                   & ~((bfd_vma) - 1
  487.                   >> (howto->rightshift - howto->bitpos)));
  488.             if (((bfd_vma) check & ~reloc_bits) != (-1 & ~reloc_bits))
  489.               flag = bfd_reloc_overflow;
  490.           }
  491.         else
  492.           flag = bfd_reloc_overflow;
  493.           }
  494.       }
  495.       break;
  496.     default:
  497.       abort ();
  498.     }
  499.     }
  500.  
  501.   /*
  502.     Either we are relocating all the way, or we don't want to apply
  503.     the relocation to the reloc entry (probably because there isn't
  504.     any room in the output format to describe addends to relocs)
  505.     */
  506.  
  507.   /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
  508.      (OSF version 1.3, compiler version 3.11).  It miscompiles the
  509.      following program:
  510.  
  511.      struct str
  512.      {
  513.        unsigned int i0;
  514.      } s = { 0 };
  515.  
  516.      int
  517.      main ()
  518.      {
  519.        unsigned long x;
  520.  
  521.        x = 0x100000000;
  522.        x <<= (unsigned long) s.i0;
  523.        if (x == 0)
  524.      printf ("failed\n");
  525.        else
  526.      printf ("succeeded (%lx)\n", x);
  527.      }
  528.      */
  529.  
  530.   relocation >>= (bfd_vma) howto->rightshift;
  531.  
  532.   /* Shift everything up to where it's going to be used */
  533.  
  534.   relocation <<= (bfd_vma) howto->bitpos;
  535.  
  536.   /* Wait for the day when all have the mask in them */
  537.  
  538.   /* What we do:
  539.      i instruction to be left alone
  540.      o offset within instruction
  541.      r relocation offset to apply
  542.      S src mask
  543.      D dst mask
  544.      N ~dst mask
  545.      A part 1
  546.      B part 2
  547.      R result
  548.  
  549.      Do this:
  550.      i i i i i o o o o o        from bfd_get<size>
  551.      and           S S S S S    to get the size offset we want
  552.      +   r r r r r r r r r r  to get the final value to place
  553.      and           D D D D D  to chop to right size
  554.      -----------------------
  555.      A A A A A
  556.      And this:
  557.      ...   i i i i i o o o o o  from bfd_get<size>
  558.      and   N N N N N            get instruction
  559.      -----------------------
  560.      ...   B B B B B
  561.  
  562.      And then:
  563.      B B B B B
  564.      or              A A A A A
  565.      -----------------------
  566.      R R R R R R R R R R        put into bfd_put<size>
  567.      */
  568.  
  569. #define DOIT(x) \
  570.   x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) +  relocation) & howto->dst_mask))
  571.  
  572.   switch (howto->size)
  573.     {
  574.     case 0:
  575.       {
  576.     char x = get_data (data, addr, 1);
  577.     DOIT (x);
  578.     overflow = put_data(x, data, addr, 1);
  579.       }
  580.       break;
  581.  
  582.     case 1:
  583.       if (relocation)
  584.     {
  585.       short x = get_data (data, addr, 2);
  586.       DOIT (x);
  587.       overflow = put_data(x, (unsigned char *) data, addr, 2);
  588.     }
  589.       break;
  590.     case 2:
  591.       if (relocation)
  592.     {
  593.       long x = get_data (data, addr, 4);
  594.       DOIT (x);
  595.       overflow = put_data(x, data, addr, 4);
  596.     }
  597.       break;
  598.     case -2:
  599.       {
  600.     long  x = get_data(data, addr, 4);
  601.     relocation = -relocation;
  602.     DOIT(x);
  603.     overflow = put_data(x, data , addr, 4);
  604.       }
  605.       break;
  606.  
  607.     case 3:
  608.       /* Do nothing */
  609.       break;
  610.  
  611.     case 4:
  612. #ifdef BFD64
  613.       if (relocation)
  614.     {
  615.       bfd_vma x = get_data (data, addr, 8);
  616.       DOIT (x);
  617.       overflow = put_data(x, data, addr, 8);
  618.     }
  619. #else
  620.       abort ();
  621. #endif
  622.       break;
  623.     default:
  624.       return bfd_reloc_other;
  625.     }
  626.   if ((howto->complain_on_overflow != complain_overflow_dont) && overflow)
  627.     return bfd_reloc_overflow;
  628.  
  629.   return flag;
  630. }
  631.  
  632. /* Relocate a given location using a given value and howto.  */
  633.  
  634. bfd_reloc_status_type
  635. do_ns32k_reloc_contents ( howto, input_bfd, relocation, location, get_data,
  636.              put_data)
  637.      const reloc_howto_type *howto;
  638.      bfd *input_bfd;
  639.      bfd_vma relocation;
  640.      bfd_byte *location;
  641.      long (*get_data)();
  642.      int (*put_data)();
  643. {
  644.   int size;
  645.   bfd_vma x;
  646.   boolean overflow;
  647.  
  648.   /* If the size is negative, negate RELOCATION.  This isn't very
  649.      general.  */
  650.   if (howto->size < 0)
  651.     relocation = -relocation;
  652.  
  653.   /* Get the value we are going to relocate.  */
  654.   size = bfd_get_reloc_size (howto);
  655.   switch (size)
  656.     {
  657.     default:
  658.     case 0:
  659.       abort ();
  660.     case 1:
  661.     case 2:
  662.     case 4:
  663. #ifdef BFD64
  664.     case 8:
  665. #endif
  666.       x = get_data (location, 0, size);
  667.       break;
  668.     }
  669.  
  670.   /* Check for overflow.  FIXME: We may drop bits during the addition
  671.      which we don't check for.  We must either check at every single
  672.      operation, which would be tedious, or we must do the computations
  673.      in a type larger than bfd_vma, which would be inefficient.  */
  674.   overflow = false;
  675.   if (howto->complain_on_overflow != complain_overflow_dont)
  676.     {
  677.       bfd_vma check;
  678.       bfd_signed_vma signed_check;
  679.       bfd_vma add;
  680.       bfd_signed_vma signed_add;
  681.  
  682.       if (howto->rightshift == 0)
  683.     {
  684.       check = relocation;
  685.       signed_check = (bfd_signed_vma) relocation;
  686.     }
  687.       else
  688.     {
  689.       /* Drop unwanted bits from the value we are relocating to.  */
  690.       check = relocation >> howto->rightshift;
  691.  
  692.       /* If this is a signed value, the rightshift just dropped
  693.          leading 1 bits (assuming twos complement).  */
  694.       if ((bfd_signed_vma) relocation >= 0)
  695.         signed_check = check;
  696.       else
  697.         signed_check = (check
  698.                 | ((bfd_vma) - 1
  699.                    & ~((bfd_vma) - 1 >> howto->rightshift)));
  700.     }
  701.  
  702.       /* Get the value from the object file.  */
  703.       add = x & howto->src_mask;
  704.  
  705.       /* Get the value from the object file with an appropriate sign.
  706.      The expression involving howto->src_mask isolates the upper
  707.      bit of src_mask.  If that bit is set in the value we are
  708.      adding, it is negative, and we subtract out that number times
  709.      two.  If src_mask includes the highest possible bit, then we
  710.      can not get the upper bit, but that does not matter since
  711.      signed_add needs no adjustment to become negative in that
  712.      case.  */
  713.       signed_add = add;
  714.       if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
  715.     signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
  716.  
  717.       /* Add the value from the object file, shifted so that it is a
  718.      straight number.  */
  719.       if (howto->bitpos == 0)
  720.     {
  721.       check += add;
  722.       signed_check += signed_add;
  723.     }
  724.       else
  725.     {
  726.       check += add >> howto->bitpos;
  727.  
  728.       /* For the signed case we use ADD, rather than SIGNED_ADD,
  729.          to avoid warnings from SVR4 cc.  This is OK since we
  730.          explictly handle the sign bits.  */
  731.       if (signed_add >= 0)
  732.         signed_check += add >> howto->bitpos;
  733.       else
  734.         signed_check += ((add >> howto->bitpos)
  735.                  | ((bfd_vma) - 1
  736.                 & ~((bfd_vma) - 1 >> howto->bitpos)));
  737.     }
  738.  
  739.       switch (howto->complain_on_overflow)
  740.     {
  741.     case complain_overflow_signed:
  742.       {
  743.         /* Assumes two's complement.  */
  744.         bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
  745.         bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
  746.  
  747.         if (signed_check > reloc_signed_max
  748.         || signed_check < reloc_signed_min)
  749.           overflow = true;
  750.       }
  751.       break;
  752.     case complain_overflow_unsigned:
  753.       {
  754.         /* Assumes two's complement.  This expression avoids
  755.            overflow if howto->bitsize is the number of bits in
  756.            bfd_vma.  */
  757.         bfd_vma reloc_unsigned_max =
  758.         (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  759.  
  760.         if (check > reloc_unsigned_max)
  761.           overflow = true;
  762.       }
  763.       break;
  764.     case complain_overflow_bitfield:
  765.       {
  766.         /* Assumes two's complement.  This expression avoids
  767.            overflow if howto->bitsize is the number of bits in
  768.            bfd_vma.  */
  769.         bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
  770.  
  771.         if ((check & ~reloc_bits) != 0
  772.         && (((bfd_vma) signed_check & ~reloc_bits)
  773.             != (-1 & ~reloc_bits)))
  774.           overflow = true;
  775.       }
  776.       break;
  777.     default:
  778.       abort ();
  779.     }
  780.     }
  781.  
  782.   /* Put RELOCATION in the right bits.  */
  783.   relocation >>= (bfd_vma) howto->rightshift;
  784.   relocation <<= (bfd_vma) howto->bitpos;
  785.  
  786.   /* Add RELOCATION to the right bits of X.  */
  787.   x = ((x & ~howto->dst_mask)
  788.        | (((x & howto->src_mask) + relocation) & howto->dst_mask));
  789.  
  790.   /* Put the relocated value back in the object file.  */
  791.   switch (size)
  792.     {
  793.     default:
  794.     case 0:
  795.       abort ();
  796.     case 1:
  797.     case 2:
  798.     case 4:
  799. #ifdef BFD64
  800.     case 8:
  801. #endif
  802.       put_data(x, location, 0, size);
  803.       break;
  804.     }
  805.  
  806.   return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
  807. }
  808.  
  809. bfd_reloc_status_type
  810. ns32k_reloc_disp(abfd, reloc_entry, symbol, data, input_section, output_bfd,
  811.          error_message)
  812.      bfd *abfd;
  813.      arelent *reloc_entry;
  814.      struct symbol_cache_entry *symbol;
  815.      PTR data;
  816.      asection *input_section;
  817.      bfd *output_bfd;
  818.      char **error_message;
  819. {
  820.   return do_ns32k_reloc(abfd, reloc_entry, symbol, data, input_section, output_bfd, error_message, ns32k_get_displacement, ns32k_put_displacement);
  821. }
  822.  
  823. bfd_reloc_status_type
  824. ns32k_reloc_imm (abfd, reloc_entry, symbol, data, input_section, output_bfd, error_message)
  825.      bfd *abfd;
  826.      arelent *reloc_entry;
  827.      struct symbol_cache_entry *symbol;
  828.      PTR data;
  829.      asection *input_section;
  830.      bfd *output_bfd;
  831.      char **error_message;
  832. {
  833.   return do_ns32k_reloc(abfd, reloc_entry, symbol, data, input_section, output_bfd, error_message, ns32k_get_immediate, ns32k_put_immediate);
  834. }
  835.  
  836. bfd_reloc_status_type
  837. ns32k_final_link_relocate (howto, input_bfd, input_section, contents, address, value, addend )
  838.      const reloc_howto_type *howto;
  839.      bfd *input_bfd;
  840.      asection *input_section;
  841.      bfd_byte *contents;
  842.      bfd_vma address;
  843.      bfd_vma value;
  844.      bfd_vma addend;
  845. {
  846.   bfd_vma relocation;
  847.  
  848.   /* Sanity check the address.  */
  849.   if (address > input_section->_cooked_size)
  850.     return bfd_reloc_outofrange;
  851.  
  852.   /* This function assumes that we are dealing with a basic relocation
  853.      against a symbol.  We want to compute the value of the symbol to
  854.      relocate to.  This is just VALUE, the value of the symbol, plus
  855.      ADDEND, any addend associated with the reloc.  */
  856.   relocation = value + addend;
  857.  
  858.   /* If the relocation is PC relative, we want to set RELOCATION to
  859.      the distance between the symbol (currently in RELOCATION) and the
  860.      location we are relocating.  Some targets (e.g., i386-aout)
  861.      arrange for the contents of the section to be the negative of the
  862.      offset of the location within the section; for such targets
  863.      pcrel_offset is false.  Other targets (e.g., m88kbcs or ELF)
  864.      simply leave the contents of the section as zero; for such
  865.      targets pcrel_offset is true.  If pcrel_offset is false we do not
  866.      need to subtract out the offset of the location within the
  867.      section (which is just ADDRESS).  */
  868.   if (howto->pc_relative)
  869.     {
  870.       relocation -= (input_section->output_section->vma
  871.              + input_section->output_offset);
  872.       if (howto->pcrel_offset)
  873.     relocation -= address;
  874.     }
  875.  
  876.   return ns32k_relocate_contents (howto, input_bfd, relocation,
  877.                   contents + address);
  878. }
  879.